<template>
  <div>
    <el-row style="padding-top: 40px;">
      <el-col :span="10">
        <el-form
          :model="resetPasswordForm"
          :rules="rules"
          ref="resetPasswordForm"
          label-width="100px">
          <el-form-item
            label="当前密码:"
            prop="oldPassword">
            <el-input
              type="password"
              v-model="resetPasswordForm.oldPassword">
            </el-input>
          </el-form-item>
          <el-form-item
            label="新密码:"
            prop="newPassword">
            <el-input
              type="password"
              v-model="resetPasswordForm.newPassword">
            </el-input>
          </el-form-item>
          <el-form-item
            label="重新输入:"
            prop="reNewPassword">
            <el-input
              type="password"
              v-model="resetPasswordForm.reNewPassword">
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="large"
              @click="submitPassword">
              确 定
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script >
import { mapActions } from 'vuex'

export default {
  name: 'password-reset',
  data () {
    var repasswordCheck = (rule, value, callback) => {
      if (value !== this.resetPasswordForm.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    var newpasswordCheck = (rule, value, callback) => {
      if (value === this.resetPasswordForm.oldPassword) {
        callback(new Error('新旧密码一致，请修改!'))
      } else {
        callback()
      }
    }
    return {
      resetPasswordForm: {
        oldPassword: '',
        newPassword: '',
        reNewPassword: ''
      },
      rules: {
        oldPassword: [
        {required: true, message: '请输旧入密码', trigger: 'blur'},
        {minlength: 6, maxlength: 12, message: '密码6-12位', trigger: 'blur'}
        ],
        newPassword: [
        {required: true, message: '请输新入密码', trigger: 'blur'},
        {minlength: 6, maxlength: 12, message: '密码6-12位', trigger: 'blur'},
        {validator: newpasswordCheck, message: '新旧密码一致，请修改', trigger: 'blur'}
        ],
        reNewPassword: [
        {required: true, message: '请再次输入新密码', trigger: 'blur'},
        {validator: repasswordCheck, message: '两次新密码不同', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    ...mapActions([
      'resetPassword'
    ]),
    submitPassword () {
      this.$refs['resetPasswordForm'].validate((valid) => {
        if (valid) {
          this.resetPassword(this.resetPasswordForm)
        }
      })
    }
  }
}
</script>
<style>
</style>
